package com.bird.main;

import java.util.ArrayList;
import java.util.List;

public class BarrierPool {
    public static final int initCount = 16;
    public static final int maxCount = 20;
    private static List<Barrier> pool = new ArrayList<>();

    static {
        for (int i = 0; i < initCount; i++) {
            pool.add(new Barrier());
        }
    }

    /*从容器中取出*/
    public static Barrier getFromPool() {
        int size = pool.size();
        if (size > 0) {
            System.out.println("取出一个");
            return pool.remove(size - 1);
        } else {
            System.out.println("新的对象");
            return new Barrier();
        }
    }

    /*归还给容器*/
    public static void backPool(Barrier barrier) {
        if (pool.size() < maxCount) {
            pool.add(barrier);
            System.out.println("归还至容器成功!");
        }
    }

}
